<?php

namespace App\Admin\Forms;

use App\Models\SwtMerchant;
use App\Models\SwtMerchantMachine;
use App\Models\SwtReplenish;
use App\Services\AdminService;
use Dcat\Admin\Admin;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Dcat\Admin\Contracts\LazyRenderable;
use Illuminate\Support\Facades\DB;

class ReplenishPriceForm extends Form implements LazyRenderable
{
    use LazyWidget;

    // 处理请求
    public function handle(array $input)
    {

        Db::beginTransaction();

        try {
            $deposit = SwtMerchantMachine::where('machine_id', $input['machine_id']) -> where('is_owner', 1) -> first();

            if(!$deposit) {
                throw new \Exception('未找到用户设备');
            }

            $merchant_info = SwtMerchant::where('id', $deposit['merchant_id']) -> first();

            if(!$merchant_info) {
                throw new \Exception('未找到用户信息');
            }

            if($input['amount'] > 0) {
                if($deposit -> deposit_now - $input['amount'] < 0) {
                    throw new \Exception('货款不足');
                }
            } else {
                if($deposit -> deposit_now - $input['amount'] > $merchant_info -> deposit) {
                    throw new \Exception('超出货款');
                }
            }

            $res = SwtMerchantMachine::where('id', $deposit -> id)->decrement('deposit_now', $input['amount']);

            if(!$res) {
                throw new \Exception('货款修改失败');
            }

            $input['merchant_id'] =  $deposit['merchant_id'];
            $input['admin_id'] = Admin::user()->id;
            $input['status'] = 1;

            $res = SwtReplenish::create($input);

            if(!$res) {
                throw new \Exception('日志新增失败');
            }

            if($merchant_info -> shop_user_id) {
                $params = $input;

                $params['shop_user_id'] = $merchant_info -> shop_user_id;

                $params['type'] = $input['amount'] < 0 ? 0 : 1;

                $res = AdminService::setDeviceDeposit($params);

                if(!$res) {
                    throw new \Exception('同步货款失败');
                }
            }

            Db::commit();
            return $this->response()->success('操作成功')->refresh();
        } catch (\Exception $e) {
            Db::rollBack();
            return $this->response()->error($e->getMessage());
        }
    }


    public function form()
    {
        $id = $this->payload['id'] ?? null;

        $merchant_machine_info = SwtMerchantMachine::where('machine_id', $id) -> where('is_owner', 1) -> first();

        if($merchant_machine_info) {
            $this -> hidden('machine_id') -> default($id);

            $this -> text('amount','金额')
                -> type('number')
                -> rules('required', [ 'required' => '金额不能为空'])
                -> required();
        } else {
            $this->disableResetButton();
            $this->disableSubmitButton();
            $this->html('<div class="text-center text-gray w-100"><i class="feather icon-alert-circle"></i> 暂无机主</div>');
        }
    }
}
